package com.rampage.controller;

import com.rampage.entity.Result;
import com.rampage.service.BaseService;
import com.rampage.service.BaseServiceInterface;
import com.rampage.utils.GenericsUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.Serializable;
import java.nio.file.AccessDeniedException;

/**
 *
 * @param <T>
 * @param <ID>
 */
public class BaseController<T, ID extends Serializable> {

    @Autowired
    private BaseServiceInterface<T, ID> mModelService;

    protected Logger mLogger;

    public BaseController() {
        mLogger = LogManager.getLogger(this.getClass());
    }

    public BaseServiceInterface<T, ID> getModelService() {
        return mModelService;
    }

    public void setModelService(BaseServiceInterface<T, ID> mModelService) {
        this.mModelService = mModelService;
    }

    @RequestMapping(value = "load")
    public Result load(ID id) {
        try {
            //检查权限
            T model = getModelService().load(id);
            if (model == null) {
                return buildFaildResult("没有找到此对象");
            }
            return buildSuccessResult(model);
        } catch (Exception e) {
            mLogger.error("读取失败", e);
            return buildFaildResult(e, "加载失败");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "delete")
    public Result delete(ID id) {
        try {
            //检查权限
            getModelService().deleteById(id);
            return buildSuccessResult();
        } catch (Exception e) {
            mLogger.error("删除失败", e);
            return buildFaildResult(e, obatinDeleteExciptionMessage(e));
        }
    }

    /**
     * 生成删除时报错的 提示信息
     * 处理特殊删除信息提示
     *
     * @param e
     * @return
     */
    private String obatinDeleteExciptionMessage(Exception e) {
        String msg = "删除失败";
        try {
            if (e instanceof DataIntegrityViolationException) {
                return "存在其他关联的数据，无法删除此数据";
            }
        } catch (Exception e1) {

        }
        return msg;
    }

    protected Result buildResult(int state, Object data) {
        return new Result(state, data);
    }

    protected Result buildSuccessResult(Object data) {
        return new Result(Result.STATE_SUCCESS, data);
    }

    protected Result buildSuccessResult(Object data, String message) {
        return new Result(Result.STATE_SUCCESS, message, data);
    }

    protected Result buildSuccessResult() {
        return new Result(Result.STATE_SUCCESS);
    }

    protected Result buildFaildResult() {
        return new Result(Result.STATE_FAILD);
    }

    protected Result buildFaildResult(String message) {
        return new Result(-1, message, null);
    }

    protected Result buildFaildResult(Exception e) {
        return buildFaildResult(e, "操作失败");
    }

    protected Result buildFaildResult(Exception e, String defaultmsg) {
        mLogger.error(e.getMessage(), e);

        //没有权限时报的错
        if (e.getClass().equals(AccessDeniedException.class)) {
            return buildFaildResult("你没有此操作的权限");
        }

        return buildFaildResult(defaultmsg);
    }

}
